package com.dmcb.trade.business.crawlers.article;

import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.entities.Article;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Service;

/**
 * 新浪文章解析器
 * Created by Administrator on 2016/4/28.
 */
@Service
public class XinLangParser extends BaseParser {

    /**
     * 网站URL网址
     *
     * @return 链接
     */
    @Override
    protected String url() {
        return CrawlConstant.XINLANG_URL;
    }

    /**
     * 获取标题选择器
     *
     * @return 标题选择器
     */
    @Override
    protected String titleSelect() {
        return "h1.art_tit_h1,h1.main-title,div.page-header>h1#artibodyTitle,h1#main_title,div.article-header>h1,h1#artibodyTitle,h1.title";
    }

    /**
     * 获取日期选择器
     *
     * @return 发布日期选择器
     */
    @Override
    protected String dateSelect() {
        return "span.date,span#navtimeSource,span.time-source>span.titer,span#pub_date,p.origin>span";
    }

    /**
     * 获取正文内容选择器
     *
     * @return 正文内容选择器
     */
    @Override
    protected String bodySelect() {
        return "div#artibody,div#articleContent,div.articalContent,div.article-body";
    }

    /**
     * 获取封面选择器
     *
     * @return 封面选择器
     */
    @Override
    protected String coverSelect() {
        return "div#articleContent,div#artibody>p img[src],div.img_wrapper img[src],div.article-body>p img[src],span.name>a,div.name";
    }

    /**
     * 获取作者选择器
     *
     * @return 作者选择器
     */
    @Override
    protected String authorNameSelect() {
        return "div.artibody>p,";
    }

    @Override
    protected void setAuthorName(Document doc, Article article) {
//        if (doc.select("div#artibody>p").size()>1) {
//            String authorName = doc.select("div#artibody>p").get(1).text();
//            authorName=authorName.substring(authorName.indexOf("/")+1);
//            if (StringUtils.isNotBlank(authorName)) {
//                article.setAuthorName(authorName);
//            }
//        }
        String authorName=doc.select("meta[name$=mediaid]").attr("content");
        if (StringUtils.isNotBlank(authorName)) {
                article.setAuthorName(authorName);
            }
    }
    @Override
    protected String platform() {
        return "新浪";
    }
}